package com.info.protocol.netty.client;

import com.info.protocol.netty.core.CustomFuture;
import com.info.protocol.netty.core.Protocol;
import com.info.protocol.netty.core.RequestHolder;
import com.info.protocol.netty.core.Response;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class CustomClientHandler extends SimpleChannelInboundHandler<Protocol<Response>> {
    @Override
    protected void channelRead0(ChannelHandlerContext ctx, Protocol<Response> msg) throws Exception {
        //接收 rpc 调用的结果
        log.info("receive rpc server result");
        long requestId = msg.getHeader().getMessageId();
        CustomFuture<Response> future = RequestHolder.REQUEST_MAP.remove(requestId);
        future.getPromise().setSuccess(msg.getContent()); //返回结果
    }
}
